<?php


namespace App\Services\Aliyun;


use App\Services\Configs\DefaultService;

class BaseService
{
    private $message;

    public function getMessage()
    {
        return $this->message;//返回结果为"", 可能配额已耗尽, 该信息是在响应头返回的, 响应头底下关闭了
    }

    /**
     * 获取配置信息
     * @return mixed
     * @throws \Exception
     */
    protected function getConfig($key)
    {
        $config = new DefaultService();
        $data = $config->first($key);
        if (!$data) {
            throw new \Exception('找不到配置 '.$key);
        }
        return $data;
    }

    protected function request($url, $bodys, $headers, $method = 'POST')
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        if (1 == strpos("$" . $url, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
        $data = curl_exec($curl);
        $this->message = $data;
        return $data;
    }
}
